//package com.noah.common.redis.config;
//
//import com.fasterxml.jackson.annotation.JsonAutoDetect;
//import com.fasterxml.jackson.annotation.JsonTypeInfo;
//import com.fasterxml.jackson.annotation.PropertyAccessor;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
//import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
//import io.netty.buffer.ByteBuf;
//import io.netty.buffer.ByteBufAllocator;
//import io.netty.buffer.ByteBufInputStream;
//import io.netty.buffer.ByteBufOutputStream;
//import org.redisson.client.codec.BaseCodec;
//import org.redisson.client.protocol.Decoder;
//import org.redisson.client.protocol.Encoder;
//
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.OutputStream;
//import java.time.LocalDateTime;
//import java.time.format.DateTimeFormatter;
//
///**
// * @author noah
// * 2023/9/7 18:52
// */
//public class CustomJsonJacksonCodec  extends BaseCodec {
//    public static final CustomJsonJacksonCodec INSTANCE = new CustomJsonJacksonCodec();
//
//    private volatile ObjectMapper mapObjectMapper;
//
//    public CustomJsonJacksonCodec() {
//        this.mapObjectMapper = CustomJsonJacksonCodec.getObjectMapper();
//    }
//
//    private final Encoder encoder = in -> {
//        ByteBuf out = ByteBufAllocator.DEFAULT.buffer();
//        try {
//            ByteBufOutputStream os = new ByteBufOutputStream(out);
//            mapObjectMapper.writeValue((OutputStream) os, in);
//            return os.buffer();
//        }
//        catch (IOException e) {
//            out.release();
//            throw e;
//        }
//        catch (Exception e) {
//            out.release();
//            throw new IOException(e);
//        }
//    };
//
//    private final Decoder<Object> DECODER = (buf, state) -> mapObjectMapper
//            .readValue((InputStream) new ByteBufInputStream(buf), Object.class);
//
//    @Override
//    public Decoder<Object> getValueDecoder() {
//        return DECODER;
//    }
//
//    @Override
//    public Encoder getValueEncoder() {
//        return encoder;
//    }
//
//    public static ObjectMapper getObjectMapper() {
//        // 解决查询缓存转换异常的问题
//        ObjectMapper objectMapper = new ObjectMapper();
//        DateTimeFormatter dateTimeFormatter = DateUtil.getDateTimeFormatter(DateUtil.YYYY_MM_DD_HH_MM_SS);
//        // Long类型转String类型
//        JavaTimeModule javaTimeModule = new JavaTimeModule();
//        javaTimeModule.addSerializer(Long.class, ToStringSerializer.instance);
//        javaTimeModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
//        // LocalDateTime
//        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));
//        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));
//        objectMapper.registerModule(javaTimeModule);
//        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL,
//                JsonTypeInfo.As.PROPERTY);
//        return objectMapper;
//    }
//}
